Completed
Push — master ( 34f5de...9cf608 )
by greg
45s
created

templates.js ➔ describe(ꞌcmsTemplatesꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 413

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 413
rs 8.2857
c 1
b 0
f 0

18 Functions

Rating   Name   Duplication   Size   Complexity  
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getAbeImport()ꞌ) 0 4 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getTemplatesTexts()ꞌ) 0 17 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getStructureAndTemplates()ꞌ) 0 18 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.encodeAbeTagAsComment()ꞌ) 0 4 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist()ꞌ) 0 14 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getTemplatesAndPartials()ꞌ) 0 17 1
B templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.includePartials()ꞌ) 0 36 1
A templates.js ➔ ... ➔ before 0 22 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist()ꞌ) 0 14 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.addOrder()ꞌ) 0 15 1
B templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getAbePrecontribFromTemplates()ꞌ) 0 25 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getAbeRequestWhereKeysFromTemplates()ꞌ) 0 18 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getVariablesInWhere()ꞌ) 0 9 1
B templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getTemplate()ꞌ) 0 33 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.getAbeSlugFromTemplates()ꞌ) 0 21 1
B templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.execRequestColumns()ꞌ) 0 27 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.translate()ꞌ) 0 23 1
A templates.js ➔ ... ➔ it(ꞌcmsTemplates.template.recurseWhereVariables()ꞌ) 0 9 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var chai = require('chai');
2
var sinonChai = require('sinon-chai')
3
var expect = chai.expect
4
chai.use(sinonChai)
5
var sinon = require('sinon');
6
var path = require('path');
7
var fse = require('fs-extra');
8
9
var config = require('../../../src/cli').config
10
config.set({root: path.join(process.cwd(), 'test', 'fixtures')})
11
12
var abeExtend = require('../../../src/cli').abeExtend;
13
var cmsData = require('../../../src/cli').cmsData;
14
var cmsTemplates = require('../../../src/cli').cmsTemplates;
15
var coreUtils = require('../../../src/cli').coreUtils;
16
var Manager = require('../../../src/cli').Manager;
17
18
describe('cmsTemplates', function() {
19
  before( function(done) {
20
    Manager.instance.init()
21
      .then(function () {
22
        this.fixture = {
23
          slug: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'slug.html'), 'utf-8'),
24
          article: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf-8'),
25
          local: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'local.html'), 'utf-8'),
26
          articleSingleAbe: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-single-abe.html'), 'utf-8'),
27
          articleRequest: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-request.html'), 'utf-8'),
28
          template: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'import.html'), 'utf-8'),
29
          articleEach: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-each-abe.html'), 'utf-8'),
30
          articlePrecontrib: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-precontribution.html'), 'utf-8'),
31
          templateKeys: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-keys.html'), 'utf-8'),
32
          templatePaths: path.join(process.cwd(), 'test', 'fixtures', 'templates', 'templates/article.html'),
33
          structurePaths: path.join(process.cwd(), 'test', 'fixtures', 'templates', 'templates/structure/0-1'),
34
          import: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'import.html'), 'utf-8'),
35
          count: 0
36
        }
37
        done()
38
        
39
      }.bind(this))
40
  });
41
42
  /**
43
   * cmsTemplates.template.getTemplatesAndPartials
44
   * 
45
   */
46
  it('cmsTemplates.template.getTemplatesAndPartials()', function(done) {
47
    // stub
48
    var sinonInstance = sinon.sandbox.create();
49
    var getFilesAsync = sinonInstance.stub(coreUtils.file, 'getFilesAsync');
50
    getFilesAsync.returns(Promise.resolve([this.fixture.article]))
51
52
    // test
53
    cmsTemplates.template.getTemplatesAndPartials(this._pathTemplate)
54
    .then((templatesList) => {
55
      chai.expect(templatesList.length).to.be.equal(1);
56
      done()
57
58
      // unstub
59
      sinon.assert.calledOnce(coreUtils.file.getFilesAsync)
60
      coreUtils.file.getFilesAsync.restore()
61
    })
62
  });
63
64
  /**
65
   * 
66
   * 
67
   */
68
  it('cmsTemplates.template.addOrder()', function() {
69
    // stub
70
    var sinonInstance = sinon.sandbox.create();
71
    var getAttr = sinonInstance.stub(cmsData.regex, 'getAttr');
72
    getAttr.returns(null)
73
74
    // test
75
    var html = cmsTemplates.template.addOrder(this.fixture.articleSingleAbe)
76
    chai.expect(html).to.not.be.equal(this.fixture.articleSingleAbe);
77
    chai.expect(html.indexOf('order')).to.above(-1);
78
79
    // unstub
80
    sinon.assert.calledOnce(cmsData.regex.getAttr)
81
    cmsData.regex.getAttr.restore()
82
  });
83
84
  /**
85
   * cmsTemplates.template.getStructureAndTemplates
86
   * 
87
   */
88
  it('cmsTemplates.template.getStructureAndTemplates()', function() {
89
    // stub
90
    var sinonInstance = sinon.sandbox.create();
91
    var stubGetFoldersSync = sinonInstance.stub(coreUtils.file, 'getFoldersSync');
92
    stubGetFoldersSync.returns({path: this.fixture.structurePaths, folders: []})
93
    var stubGetFilesSync = sinonInstance.stub(coreUtils.file, 'getFilesSync');
94
    stubGetFilesSync.returns([this.fixture.templatePaths])
95
96
    // test
97
    var res = cmsTemplates.template.getStructureAndTemplates()
98
    chai.expect(res.templates.length).to.be.equal(1);
99
100
    // unstub
101
    sinon.assert.calledOnce(coreUtils.file.getFoldersSync)
102
    coreUtils.file.getFoldersSync.restore()
103
    sinon.assert.calledOnce(coreUtils.file.getFilesSync)
104
    coreUtils.file.getFilesSync.restore()
105
  });
106
107
  /**
108
   * getAbeImport
109
   * 
110
   */
111
  it('cmsTemplates.template.getAbeImport()', function() {
112
    var res = cmsTemplates.template.getAbeImport(this.fixture.template)
113
    chai.expect(res).to.have.length(1);
114
  });
115
116
  /**
117
   * translate
118
   * 
119
   */
120
  it('cmsTemplates.template.translate()', function() {
121
    // stub
122
    var sinonInstance = sinon.sandbox.create();
123
    var getAttr = sinonInstance.stub(cmsData.regex, 'getAttr', function (math, type) {
124
      if (type === 'locale') {
125
        return 'gb'
126
      }
127
      if (type === 'source') {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if type === "source" is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
128
        return 'test'
129
      }
130
    });
131
    var escapeTextToRegex = sinonInstance.stub(cmsData.regex, 'escapeTextToRegex');
132
    escapeTextToRegex.returns(new RegExp(this.fixture.local, 'g'))
133
134
    // test
135
    var res = cmsTemplates.template.translate(this.fixture.local)
136
    chai.expect(res.indexOf('i18nAbe')).to.above(-1);
137
138
    // unstub
139
    cmsData.regex.getAttr.restore()
140
    sinon.assert.calledOnce(cmsData.regex.escapeTextToRegex)
141
    cmsData.regex.escapeTextToRegex.restore()
142
  });
143
144
  /**
145
   * getVariablesInWhere
146
   * 
147
   */
148
  it('cmsTemplates.template.getVariablesInWhere()', function() {
149
    // stub
150
151
    // test
152
    var res = cmsTemplates.template.getVariablesInWhere({left: {column: 'title'}, right: {value: "test"}, operator: 'AND'})
153
    chai.expect(res.length).to.above(-1);
154
155
    // unstub
156
  });
157
158
  /**
159
   * recurseWhereVariables
160
   * 
161
   */
162
  it('cmsTemplates.template.recurseWhereVariables()', function() {
163
    // stub
164
165
    // test
166
    var res = cmsTemplates.template.recurseWhereVariables({left: {column: 'title'}, right: {value: "test"}})
167
    chai.expect(res.length).to.above(-1);
168
169
    // unstub
170
  });
171
172
  /**
173
   * getTemplatesTexts
174
   * 
175
   */
176
  it('cmsTemplates.template.getTemplatesTexts()', function(done) {
177
    // stub
178
    var sinonInstance = sinon.sandbox.create();
179
    var readFileSync = sinonInstance.stub(fse, 'readFileSync');
180
    readFileSync.returns("test")
181
182
    // test
183
    cmsTemplates.template.getTemplatesTexts(['test'])
184
    .then(function (res) {
185
      chai.expect(res[0].name).to.be.equal('test');
186
187
      // unstub
188
      sinon.assert.calledOnce(fse.readFileSync)
189
      fse.readFileSync.restore()
190
      done()
191
    })
192
  });
193
194
  /**
195
   * includePartials
196
   * 
197
   */
198
  it('cmsTemplates.template.includePartials()', function() {
199
    // stub
200
    var sinonInstance = sinon.sandbox.create();
201
    var stubReadFileSync = sinonInstance.stub(fse, 'readFileSync');
202
    stubReadFileSync.returns("test")
203
    var stubGetAbeImport = sinonInstance.stub(cmsTemplates.template, 'getAbeImport', function () {
0 ignored issues
show
Unused Code introduced by
The variable stubGetAbeImport seems to be never used. Consider removing it.
Loading history...
204
      if (this.fixture.count === 0) {
205
        this.fixture.count++
206
        return [this.fixture.import]
207
      }
208
      return []
209
    }.bind(this));
210
    var stubGetAll = sinonInstance.stub(cmsData.attributes, 'getAll');
211
    stubGetAll.returns({file: 'import.html'})
212
    var stubEscapeTextToRegex = sinonInstance.stub(cmsData.regex, 'escapeTextToRegex');
213
    stubEscapeTextToRegex.returns(new RegExp(this.fixture.import, 'g'))
214
    var stubFile = sinonInstance.stub(coreUtils.file, 'exist', function () {
0 ignored issues
show
Unused Code introduced by
The variable stubFile seems to be never used. Consider removing it.
Loading history...
215
      return true
216
    }.bind(this));
0 ignored issues
show
unused-code introduced by
The call to bind does not seem necessary since the function does not use this. Consider calling it directly.
Loading history...
217
218
    // test
219
    var template = cmsTemplates.template.includePartials(this.fixture.template)
220
    chai.expect(template).to.be.equal("test");
221
222
    // unstub
223
    sinon.assert.calledOnce(fse.readFileSync)
224
    fse.readFileSync.restore()
225
    sinon.assert.calledTwice(cmsTemplates.template.getAbeImport)
226
    cmsTemplates.template.getAbeImport.restore()
227
    sinon.assert.calledOnce(cmsData.attributes.getAll)
228
    cmsData.attributes.getAll.restore()
229
    sinon.assert.calledOnce(coreUtils.file.exist)
230
    coreUtils.file.exist.restore()
231
    sinon.assert.calledOnce(cmsData.regex.escapeTextToRegex)
232
    cmsData.regex.escapeTextToRegex.restore()
233
  });
234
235
  /**
236
   * cmsTemplates.template.getTemplate
237
   * 
238
   */
239
  it('cmsTemplates.template.getTemplate()', function() {
240
    // stub
241
    var sinonInstance = sinon.sandbox.create();
242
    var stubReadFileSync = sinonInstance.stub(fse, 'readFileSync');
243
    stubReadFileSync.returns(this.fixture.article)
244
    var stubTrigger = sinonInstance.stub(abeExtend.hooks.instance, 'trigger', function (p1, p2) {
0 ignored issues
show
Unused Code introduced by
The variable stubTrigger seems to be never used. Consider removing it.
Loading history...
245
      return p2
246
    })
247
    var stubExist = sinonInstance.stub(coreUtils.file, 'exist');
248
    stubExist.returns(true)
249
    var stubIncludePartials = sinonInstance.stub(cmsTemplates.template, 'includePartials');
250
    stubIncludePartials.returns(this.fixture.article)
251
    var stubTranslate = sinonInstance.stub(cmsTemplates.template, 'translate');
252
    stubTranslate.returns(this.fixture.article)
253
    var stubAddOrder = sinonInstance.stub(cmsTemplates.template, 'addOrder');
254
    stubAddOrder.returns(this.fixture.article)
255
256
    var template = cmsTemplates.template.getTemplate('article')
257
    chai.expect(template).to.be.equal(this.fixture.article);
258
259
    sinon.assert.calledOnce(fse.readFileSync)
260
    fse.readFileSync.restore()
261
    sinon.assert.calledTwice(abeExtend.hooks.instance.trigger)
262
    abeExtend.hooks.instance.trigger.restore()
263
    sinon.assert.calledOnce(coreUtils.file.exist)
264
    coreUtils.file.exist.restore()
265
    sinon.assert.calledOnce(cmsTemplates.template.includePartials)
266
    cmsTemplates.template.includePartials.restore()
267
    sinon.assert.calledOnce(cmsTemplates.template.translate)
268
    cmsTemplates.template.translate.restore()
269
    sinon.assert.calledOnce(cmsTemplates.template.addOrder)
270
    cmsTemplates.template.addOrder.restore()
271
  });
272
273
  /**
274
   * cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist
275
   * 
276
   */
277
  it('cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist()', function() {
278
    // stub
279
    var sinonInstance = sinon.sandbox.create();
280
    var stubGetTagAbeWithType = sinonInstance.stub(cmsData.regex, 'getTagAbeWithType');
281
    stubGetTagAbeWithType.returns(null)
282
283
    // test
284
    var text = cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist(this.fixture.slug)
285
    chai.expect(text).to.not.be.equal(this.fixture.slug);
286
287
    // unstub
288
    sinon.assert.calledOnce(cmsData.regex.getTagAbeWithType)
289
    cmsData.regex.getTagAbeWithType.restore()
290
  });
291
292
  /**
293
   * cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist
294
   * 
295
   */
296
  it('cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist()', function() {
297
    // stub
298
    var sinonInstance = sinon.sandbox.create();
299
    var stubGetTagAbeWithTab = sinonInstance.stub(cmsData.regex, 'getTagAbeWithTab');
300
    stubGetTagAbeWithTab.returns(null)
301
302
    // test
303
    var text = cmsTemplates.template.setAbePrecontribDefaultValueIfDoesntExist(this.fixture.slug)
304
    chai.expect(text).to.not.be.equal(this.fixture.slug);
305
306
    // unstub
307
    sinon.assert.calledOnce(cmsData.regex.getTagAbeWithTab)
308
    cmsData.regex.getTagAbeWithTab.restore()
309
  });
310
311
  /**
312
   * cmsTemplates.template.getAbePrecontribFromTemplates
313
   * 
314
   */
315
  it('cmsTemplates.template.getAbePrecontribFromTemplates()', function() {
316
    // stub
317
    var sinonInstance = sinon.sandbox.create();
318
    var stubGetTagAbeWithTab = sinonInstance.stub(cmsData.regex, 'getTagAbeWithTab');
319
    stubGetTagAbeWithTab.returns(['{{abe type="slug" source="{{name}}"}}'])
320
321
    var stubGetAll = sinonInstance.stub(cmsData.attributes, 'getAll');
322
    stubGetAll.returns({name: 'test.html'})
323
324
    var stubAddOrder = sinonInstance.stub(cmsTemplates.template, 'addOrder', function (text) {
0 ignored issues
show
Unused Code introduced by
The variable stubAddOrder seems to be never used. Consider removing it.
Loading history...
325
      return text
326
    });
327
328
    // test
329
    var precontrib = cmsTemplates.template.getAbePrecontribFromTemplates([{name: 'slug', template: this.fixture.slug}])
330
    chai.expect(precontrib.fields[0]).to.not.be.undefined;
0 ignored issues
show
introduced by
The result of the property access to chai.expect(precontrib.f....0).to.not.be.undefined is not used.
Loading history...
331
332
    // unstub
333
    sinon.assert.calledOnce(cmsData.regex.getTagAbeWithTab)
334
    cmsData.regex.getTagAbeWithTab.restore()
335
    sinon.assert.calledOnce(cmsData.attributes.getAll)
336
    cmsData.attributes.getAll.restore()
337
    sinon.assert.calledOnce(cmsTemplates.template.addOrder)
338
    cmsTemplates.template.addOrder.restore()
339
  });
340
341
  /**
342
   * cmsTemplates.template.getAbeSlugFromTemplates
343
   * 
344
   */
345
  it('cmsTemplates.template.getAbeSlugFromTemplates()', function() {
346
    // stub
347
    var sinonInstance = sinon.sandbox.create();
348
    var setAbeSlugDefaultValueIfDoesntExist = sinonInstance.stub(cmsTemplates.template, 'setAbeSlugDefaultValueIfDoesntExist');
349
    setAbeSlugDefaultValueIfDoesntExist.returns(this.fixture.slug)
350
    var getTagAbeWithType = sinonInstance.stub(cmsData.regex, 'getTagAbeWithType');
351
    getTagAbeWithType.returns(['{{abe type="slug" source="{{name}}"}}'])
352
    var getAll = sinonInstance.stub(cmsData.attributes, 'getAll');
353
    getAll.returns({sourceString: 'test.html', name: 'test'})
354
355
    var slug = cmsTemplates.template.getAbeSlugFromTemplates([{name: 'slug', template: this.fixture.slug}])
356
    chai.expect(slug.slug).to.not.be.undefined;
0 ignored issues
show
introduced by
The result of the property access to chai.expect(slug.slug).to.not.be.undefined is not used.
Loading history...
357
358
    // unstub
359
    sinon.assert.calledOnce(cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist)
360
    cmsTemplates.template.setAbeSlugDefaultValueIfDoesntExist.restore()
361
    sinon.assert.calledOnce(cmsData.regex.getTagAbeWithType)
362
    cmsData.regex.getTagAbeWithType.restore()
363
    sinon.assert.calledOnce(cmsData.attributes.getAll)
364
    cmsData.attributes.getAll.restore()
365
  });
366
367
  /**
368
   * getTemplate
369
   * 
370
   */
371
  it('cmsTemplates.template.execRequestColumns()', function() {
372
    // stub
373
    var sinonInstance = sinon.sandbox.create();
374
    var getTagAbeTypeRequest = sinonInstance.stub(cmsData.regex, 'getTagAbeTypeRequest');
375
    getTagAbeTypeRequest.returns([this.fixture.articleRequest])
376
    var getSourceType = sinonInstance.stub(cmsData.sql, 'getSourceType');
377
    getSourceType.returns('request')
378
    var handleSqlRequest = sinonInstance.stub(cmsData.sql, 'handleSqlRequest');
379
    handleSqlRequest.returns({columns: ['title'], where: {left: 'title'}})
380
    var recurseWhereVariables = sinonInstance.stub(cmsTemplates.template, 'recurseWhereVariables');
381
    recurseWhereVariables.returns('title2')
382
383
    // test
384
    const pathTemplate = path.join(config.root, config.templates.url)
0 ignored issues
show
Unused Code introduced by
The constant pathTemplate seems to be never used. Consider removing it.
Loading history...
385
    var ar = cmsTemplates.template.execRequestColumns(this.fixture.templateKeys)
386
    chai.expect(ar.indexOf('title')).to.be.above(-1);
387
388
    // unstub
389
    sinon.assert.calledOnce(cmsData.regex.getTagAbeTypeRequest)
390
    cmsData.regex.getTagAbeTypeRequest.restore()
391
    sinon.assert.calledOnce(cmsData.sql.getSourceType)
392
    cmsData.sql.getSourceType.restore()
393
    sinon.assert.calledOnce(cmsData.sql.handleSqlRequest)
394
    cmsData.sql.handleSqlRequest.restore()
395
    sinon.assert.calledOnce(cmsTemplates.template.recurseWhereVariables)
396
    cmsTemplates.template.recurseWhereVariables.restore()
397
  });
398
399
  /**
400
   * getTemplate
401
   * 
402
   */
403
  it('cmsTemplates.template.getAbeRequestWhereKeysFromTemplates()', function(done) {
404
    // stub
405
    var sinonInstance = sinon.sandbox.create();
406
    var execRequestColumns = sinonInstance.stub(cmsTemplates.template, 'execRequestColumns');
407
    execRequestColumns.returns(['title'])
408
409
    // test
410
    const pathTemplate = path.join(config.root, config.templates.url)
0 ignored issues
show
Unused Code introduced by
The constant pathTemplate seems to be never used. Consider removing it.
Loading history...
411
    cmsTemplates.template.getAbeRequestWhereKeysFromTemplates([this.fixture.articleRequest])
412
    .then(function (ar) {
413
      chai.expect(ar.indexOf('title')).to.be.above(-1);
414
415
      // unstub
416
      sinon.assert.calledOnce(cmsTemplates.template.execRequestColumns)
417
      cmsTemplates.template.execRequestColumns.restore()
418
      done()
419
    })
420
  });
421
422
  /**
423
   * cmsTemplates.encodeAbeTagAsComment
424
   * 
425
   */
426
  it('cmsTemplates.encodeAbeTagAsComment()', function() {
427
    var txt = cmsTemplates.encodeAbeTagAsComment(this.fixture.articleEach);
428
    chai.expect(txt.indexOf('{')).to.equal(-1);
429
  });
430
});
431